package com.gmail.huxiaowi.cblog.service.impl;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.gmail.huxiaowi.cblog.consts.CBlogConsts;
import com.gmail.huxiaowi.cblog.dao.IArticleDao;
import com.gmail.huxiaowi.cblog.dao.ICategoryDao;
import com.gmail.huxiaowi.cblog.dao.ICommentDao;
import com.gmail.huxiaowi.cblog.dao.IMessageDao;
import com.gmail.huxiaowi.cblog.entity.Article;
import com.gmail.huxiaowi.cblog.entity.Category;
import com.gmail.huxiaowi.cblog.entity.Comment;
import com.gmail.huxiaowi.cblog.entity.Message;
import com.gmail.huxiaowi.cblog.service.IUserService;
import com.gmail.huxiaowi.cblog.util.ConstantPool;
import com.gmail.huxiaowi.cblog.util.JCacheUtils;
import com.gmail.huxiaowi.cblog.util.PageInfo;

public class UserServiceImpl implements IUserService {

    /* (non-Javadoc)
     * @see com.gmail.huxiaowi.cblog.biz.IUserBiz#getAllArticleSize()
     */
    public int getAllArticleSize() {
        return (int) articleDao.listSize();
    }

    public boolean changeArticlesCategory(Long articleId, Long categoryId) {
        Article article = articleDao.queryById(articleId);
        Category newCategory = categoryDao.queryById(categoryId);
        newCategory.addArticle(article);
        
        return true;
    }

    public boolean comment2Message(Long messageId, Comment comment) {
        Message message = messageDao.queryById(messageId);
        message.addComment(comment);
        return true;
    }

    public boolean deleteArticleById(Long id) {
        Article article = articleDao.queryById(id);
        articleDao.delete(article);
        //categoryDao.update(category);
        return true;
    }

    public boolean deleteCategory(Long id) {
        Category category = categoryDao.queryById(id);
        
        Collection<Article> articles = articleDao.queryByCategory(id);
        for (Article a: articles) {
            a.setCategoryId(null);
        }
        categoryDao.delete(category);
        
        JCacheUtils.put(CBlogConsts.CACHED_KEY_CATALOGS, queryAllCategorys());
        return true;
    }

    public boolean deleteComment4Message(Long commentId) {
        Comment comment = commentDao.queryById(commentId);
        commentDao.delete(comment);
        return true;
    }

    public boolean deleteMessage4Article(Long messageId) {
        Message message = messageDao.queryById(messageId);
        messageDao.delete(message);
        return true;
    }

    public boolean leaveMessage2Article(Long articleId, Message message) {
        Article article = articleDao.queryById(articleId);
        article.addMessage(message);
        
        return true;
    }

    public boolean modifyArticleById(Long id, Long cid, Article article) {
        Article artc = articleDao.queryById(id);
        Category category = categoryDao.queryById(cid);
        artc.setCategoryId(category.getId());
        artc.setTitle(article.getTitle());
        artc.setContent(article.getContent());
        artc.setDescription(article.getDescription());
        return true;
    }

    public boolean createCategory(Category category) {
        categoryDao.insert(category);
        JCacheUtils.put(CBlogConsts.CACHED_KEY_CATALOGS, queryAllCategorys());
        return true;
    }

    public Article queryArticleByArticleId(Long articleId, String ip) {
        Article article = articleDao.queryById(articleId);
        
        article.addIP(ip);
        return article;
    }

    public Collection<Article> queryArticlesByCategoryId(Long categoryId) {
        Category category = categoryDao.queryById(categoryId);
        return articleDao.queryByCategory(category, 0, 10);
    }

    public Collection<Article> queryAllArticles(int pageNo) {
        int totalArticles = getAllArticleSize();
        
        PageInfo pageInfo = new PageInfo(totalArticles, ConstantPool.PER_PAGE_RECORDS);
        pageInfo.setLocalPageNo(pageNo);
        
        return articleDao.queryAll(pageInfo.getStartRecord(), pageInfo.getEndRecord());//
    }
    
    public List<Category> queryAllCategorys() {
        return new ArrayList<Category>(categoryDao.queryAll());
    }

    public Collection<Comment> queryCommentsByMessageId(Long messageId) {
        Message message = messageDao.queryById(messageId);
        return commentDao.queryByMessage(message);
    }

    public Collection<Message> queryMessagesByArticleId(Long articleId) {
        Article article = articleDao.queryById(articleId);
        return messageDao.queryByArticle(article);
    }

    public boolean saveArticle(Long categoryId, Article article) {
//        Category category = categoryDao.queryById(categoryId);
//        category.addArticle(article);
        article.setCategoryId((Long)categoryId);
        articleDao.insert(article);
        return true;
    }

    public boolean renameCategory(Long categoryId, String newName) {
        Category oldCategory = categoryDao.queryById(categoryId);
        oldCategory.setName(newName);
        JCacheUtils.put(CBlogConsts.CACHED_KEY_CATALOGS, queryAllCategorys());
        
        return true;
    }

    public Collection<Map<String, String>> queryAllCategoryList() {
        Collection<Map<String, String>> categoryList = new ArrayList<Map<String, String>>();
        
        Map<String, String> defaultCategory = new HashMap<String, String>(1);
        defaultCategory.put("value", "-1");
        defaultCategory.put("text", "默认分类");
        categoryList.add(defaultCategory);
        
        Collection<Category> categorys = categoryDao.queryAll();
        for (Category c: categorys) {
            Map<String, String> map = new HashMap<String, String>(1);
            map.put("value", c.getId().toString());
            map.put("text", c.getName());
            categoryList.add(map);
        }
        
        return categoryList;
    }
    
    public Category queryCategoryById(Long categoryId) {
        return categoryDao.queryById(categoryId);
    }
    
    public UserServiceImpl() {
    }
    
    public void setCategoryDao(ICategoryDao categoryDao) {
        this.categoryDao = categoryDao;
    }
    
    public void setArticleDao(IArticleDao articleDao) {
        this.articleDao = articleDao;
    }

    public void setMessageDao(IMessageDao messageDao) {
        this.messageDao = messageDao;
    }

    public void setCommentDao(ICommentDao commentDao) {
        this.commentDao = commentDao;
    }
    
    private ICategoryDao categoryDao;
    
    private IArticleDao articleDao;
    
    private IMessageDao messageDao;
    
    private ICommentDao commentDao;

    @Override
    public Article queryArticleByPostName(String postName) {
        return articleDao.queryByPostName(postName);
    }
}
